#!/bin/sh
#
# run -- Run the docker image of the lab
#

TOP_DIR=$(cd $(dirname $0)/../../ && pwd)
. $TOP_DIR/tools/docker/config $* >/dev/null

# Check container conflicts
[ -n "$CONTAINER_ID" ] \
    && (docker ps -f id=$CONTAINER_ID | grep -v PORTS) \
    && echo "LOG: Please remove the old one with 'docker rm $CONTAINER_ID' before create new." \
    && echo "LOG: $LAB_CONTAINER_ID" \
    && exit 1

# Generate an unique container name
if [ -z "$CONTAINER_NAME" ]; then
  while :;
  do
    CONTAINER_NAME=${LAB_NAME}-`get_random`
    exist=`docker ps -q --filter=name=$CONTAINER_NAME | wc -l`
    [ $exist -eq 0 ] && break
  done
fi

# Require to prepare some environment for docker containers in host
[ -f $LAB_HOST_RUN ] && $LAB_HOST_RUN

# Run the lab via start a lab container
caps=""
devs=""
vars="-e LAB_SECURITY=$LAB_SECURITY"
envs=""
volumemap="-v $GIT_DIR:$GIT_WORKDIR"
volumemap="$volumemap -v $LAB_DIR:$LAB_WORKDIR"
volumemap="$volumemap -v $TOOL_DIR:$TOOL_WORKDIR"
volumemap="$volumemap -v $CONFIG_DIR:$CONFIG_WORKDIR"
limits=$LIMITS

for var in $VARS; do
    __ENCRYPT_CMD="cat"
    # Available encrypt cmds: sha1sum, sha224sum, cksum, sha256sum, sha512sum, md5sum, sha384sum, sum
    value=$(eval echo \$${var})
    echo "$var" | grep -q "_PWD"
    [ $? -eq 0 -a -n "$ENCRYPT" -a -n "$ENCRYPT_CMD" -a -n "$value" ] && __ENCRYPT_CMD=$ENCRYPT_CMD
    vars="$vars -e $var=$(echo -n $value | $__ENCRYPT_CMD | cut -d' ' -f1)";
done
for env in $ENVS; do vars="$vars -e $env"; done
for cap in $CAPS; do caps="$caps --cap-add $cap"; done
for dev in $DEVICES; do devs="$devs --device $dev"; done
for map in $PORTMAP; do portmap="$portmap -p $map"; done
for map in $VOLUMEMAP; do volumemap="$volumemap -v $map"; done

container="--name $CONTAINER_NAME"

# Sync UID before running
UNIX_UID=`id -u $HOST_USER`
[ "x$UNIX_UID" = "x0" -o -z "$UNIX_UID" ] && UNIX_UID=1000

do_unlock
set_var UNIX_UID
do_lock

# Start the transparent proxy via tinylab/cloud-ubuntu-proxy_client_transparent container
exist=`docker ps -q --filter=name=cloud-ubuntu-proxy_client_transparent | wc -l`
[ $exist -eq 0 -a $TPROXY -eq 1 -a -n "$PROXY_SERVER" -a -n "$PROXY_PWD" -a -n "$ENCRYPT_CMD" ] \
    && PROXY_SERVER=$PROXY_SERVER PROXY_PWD=$PROXY_PWD ENCRYPT_CMD=$ENCRYPT_CMD PROXY_LIMIT=$PROXY_LIMIT $DOCKER_TPROXY_CMD $*

# Start the ssh/vnc proxy via tinylab/cloud-ubuntu-web container
exist=`docker ps -q --filter=name=cloud-ubuntu-web | wc -l`
[ $exist -eq 0 ] && $DOCKER_EXPORT_CMD $*

CONTAINER_ID=$(docker run -d --privileged $container $portmap $caps $devices $limits $volumemap $vars $EXTRA_ARGS $IMAGE)

CONTAINER_ID=`echo $CONTAINER_ID | cut -c-12`

echo "LOG: Wait for lab launching..."
while :; do
    password=`docker logs $CONTAINER_ID 2>/dev/null | grep Password`
    sleep 2
    [ -n "$password" ] && break
done


echo "LOG: Container ID: ${CONTAINER_ID} Container Name: ${CONTAINER_NAME}"
echo "LOG: $password"

do_unlock

# Save the lab's information (for restore the container for 'docker start')
vars="$LAB_VARS"
for var in $vars; do set_var $var; done

do_lock

$DOCKER_PUBLISH_CMD $*

# $DOCKER_WEBSSH_CMD $*

$DOCKER_VNC_CMD $*
